#!/usr/bin/python

import os, sys, time
import ConfigParser
from thrift import Thrift  
from thrift.transport import TSocket  
from thrift.transport import TTransport  
from thrift.protocol import TBinaryProtocol
from cassandra import Cassandra
from cassandra.ttypes import *
from protoBuff_decode import *
from google.appengine.datastore import entity_pb

DEBUG = True

if __name__ == '__main__':
    
    thrift_server_str = '128.111.55.223'
    thrift_port_int = 9160
    
    print 'Connecting to ' + thrift_server_str + ':' + str(thrift_port_int)
    transport = TTransport.TBufferedTransport(TSocket.TSocket(thrift_server_str, thrift_port_int))
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Cassandra.Client(protocol)
    transport.open()
    print 'Connected!'
    
    keyspace = "Keyspace1"
    column_parent = ColumnParent(column_family="Standard1")
   
    keyspace_dict = client.describe_keyspace(keyspace)
    for k, v in keyspace_dict.items():
        print 'Column Family: ' + str(k)
        for k1, v1 in v.items():
            print '    ' + k1 + ' : ' + v1.replace('\n', '\n           ')
    print '======================================================================'
 
    predicate = SlicePredicate(column_names=['Encoded_Entity', 'Txn_Num'])
    key_range = KeyRange("ggreetingtests___Greeting___/", "ggreetingtests___Greeting___/~")
    start_key = "ggreetingtests___Greeting___/"
    end_key = "ggreetingtests___Greeting___/~"
    keyslices = client.get_range_slice(
		keyspace, 
                column_parent, 
                predicate,
                start_key,
		end_key,
		100000000,
                ConsistencyLevel.QUORUM)
         
    for keyslice in keyslices:
	'''
        print 'key = ' + str(keyslice.key)
        print 'columns = ' + str(keyslice.columns)
        print '----------------------------------------------'
        '''
    print 'entity number: ' + str(len(keyslices))   
 
    print 'done!'
   
